<%@ Page Language="C#" MasterPageFile="~/MasterPages/Main1.Master" AutoEventWireup="true" Codebehind="ExtendedProperties.aspx.cs"
    Inherits="NSFxSamples.ExtProperties.ExtendedProperties" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

        <div>
            Start with an example - let us say we have some Product Categories and we would
            like to return these Category classes but at the same time have the number of Products
            in the category stored with it. This is where extended properties come in handy.
            <p>
            </p>
            So start with the your generated class - first we will add a property to hold this
            new value to the class
            <pre class="code">        <span style="color: rgb(0,0,255)">private</span> <span
                style="color: rgb(0,0,255)">int</span> _productCount = <span style="color: rgb(0,0,255)">int</span>.MinValue;
        <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">int</span> ProductCount
        {
            <span style="color: rgb(0,0,255)">get </span>            {
                <span style="color: rgb(0,0,255)">if</span> (_productCount == <span style="color: rgb(0,0,255)">int</span>.MinValue)
                {
                    <span style="color: rgb(0,0,255)">this</span>.FillExtendedProperties();
                }

                <span style="color: rgb(0,0,255)">return</span> _productCount;
            }
        }</pre>
            <p>
            </p>
            Now that we have the property in class, lets define what the <i>FillExtendedProperties</i>
            method could look like
            <pre class="code">        <span style="color: rgb(0,0,255)">private</span> <span
                style="color: rgb(0,0,255)">void</span> FillExtendedProperties()
        {
            <span style="color: rgb(0,0,255)">using</span> (<span style="color: rgb(43,145,175)">SqlHelper</span> helper = <span
                style="color: rgb(0,0,255)">new</span> <span style="color: rgb(43,145,175)">SqlHelper</span>())
            {
                <span style="color: rgb(0,0,255)">string</span> commandText = <span style="color: rgb(163,21,21)">"GetProductCategoryExtendedProperties"</span>;

                <span style="color: rgb(43,145,175)">List</span>&lt;<span style="color: rgb(43,145,175)">SqlParameter</span>&gt; parameters = <span
                    style="color: rgb(0,0,255)">new</span> <span style="color: rgb(43,145,175)">List</span>&lt;<span
                        style="color: rgb(43,145,175)">SqlParameter</span>&gt;();
                parameters.Add(<span style="color: rgb(0,0,255)">new</span> <span style="color: rgb(43,145,175)">SqlParameter</span>(<span
                    style="color: rgb(163,21,21)">"@ProductCategoryId"</span>, <span style="color: rgb(0,0,255)">this</span>.ProductCategoryID))

                <span style="color: rgb(0,0,255)">using</span> (<span style="color: rgb(43,145,175)">IDataReader</span> reader = helper.ExecuteDataReader(commandText, <span
                    style="color: rgb(43,145,175)">CommandType</span>.StoredProcedure, parameters))
                {
                    <span style="color: rgb(0,0,255)">if</span> (reader.Read())
                        <span style="color: rgb(0,0,255)">this</span>.FillExtendedProperties(reader);
                }
            }
        }</pre>
            This will handle the case where we have a ProductCategory object that was retrieved
            by a query not including the custom field. Notice that we are calling an overload
            of this method, this will perform the actual assignment for the internal members.
            <pre class="code">        <span style="color: rgb(0,0,255)">private</span> <span
                style="color: rgb(0,0,255)">void</span> FillExtendedProperties(<span style="color: rgb(43,145,175)">IDataReader</span> reader)
        {
            <span style="color: rgb(0,0,255)">if</span> (!<span style="color: rgb(0,0,255)">int</span>.TryParse(reader[<span
                style="color: rgb(163,21,21)">"ProductCount"</span>].ToString(), <span style="color: rgb(0,0,255)">out</span> <span
                    style="color: rgb(0,0,255)">this</span>._productCount))
                <span style="color: rgb(0,0,255)">this</span>._productCount = 0;
        }</pre>
            Their method requires that the reader being passed in be populated by a query returning
            our custom property.<br />
            The final thing is youll need to add a static method which returns a list of ProductCategory
            objects from a stored procedure that supports the custom property. Such a method
            could look like such:
            <pre class="code">        <span style="color: rgb(0,0,255)">public</span> <span style="color: rgb(0,0,255)">static</span> <span
                style="color: rgb(43,145,175)">EntityList</span>&lt;<span style="color: rgb(43,145,175)">ProductCategory</span>&gt; GetFullProductCategoryList()
        {
            <span style="color: rgb(0,0,255)">using</span> (<span style="color: rgb(43,145,175)">SqlHelper</span> helper = <span
                style="color: rgb(0,0,255)">new</span> <span style="color: rgb(43,145,175)">SqlHelper</span>())
            {
                <span style="color: rgb(0,0,255)">string</span> commandText = <span style="color: rgb(163,21,21)">"GetFullProductCategoryList"</span>;
                <span style="color: rgb(43,145,175)">EntityList</span>&lt;<span style="color: rgb(43,145,175)">ProductCategory</span>&gt; returnList = <span
                    style="color: rgb(0,0,255)">new</span> <span style="color: rgb(43,145,175)">EntityList</span>&lt;<span
                        style="color: rgb(43,145,175)">ProductCategory</span>&gt;();
                
                <span style="color: rgb(0,0,255)">using</span> (<span style="color: rgb(43,145,175)">IDataReader</span> reader = helper.ExecuteDataReader(commandText, <span
                    style="color: rgb(43,145,175)">CommandType</span>.StoredProcedure, <span style="color: rgb(0,0,255)">new</span> <span
                        style="color: rgb(43,145,175)">List</span>&lt;<span style="color: rgb(43,145,175)">SqlParameter</span>&gt;))
                {
                    <span style="color: rgb(0,0,255)">while</span> (reader.Read())
                    {
                        <span style="color: rgb(43,145,175)">ProductCategory</span> pCategory = <span style="color: rgb(0,0,255)">new</span> <span
                            style="color: rgb(43,145,175)">ProductCategory</span>();
                        pCategory.Initialize(reader);
                        pCategory.FillExtendedProperties(reader);
                        returnList.Add(pCategory);
                    }
                }
            }
        }</pre>
        </div>

</asp:Content>